<script src="src/lib/all.js"></script>
<script id="worker_prettify" type="javascript/worker">
  self.onmessage = function(e) {
    if(e.data=='before'){
      self.postMessage('after');
    }
  };
</script>
<script>
  @@include("src/AppBundle.js")
</script>
<script>
  if (/https/.test(location.protocol) && 'serviceWorker' in navigator) {
    if (!Cache.prototype.add) {
      Cache.prototype.add = function add(request) {
        return this.addAll([request]);
      };
    }

    if (!Cache.prototype.addAll) {
      Cache.prototype.addAll = function addAll(requests) {
        var cache = this;

        // Since DOMExceptions are not constructable:
        function NetworkError(message) {
          this.name = 'NetworkError';
          this.code = 19;
          this.message = message;
        }
        NetworkError.prototype = Object.create(Error.prototype);

        return Promise.resolve().then(function() {
          if (arguments.length < 1) throw new TypeError();

          // Simulate sequence<(Request or USVString)> binding:
          var sequence = [];

          requests = requests.map(function(request) {
            if (request instanceof Request) {
              return request;
            }
            else {
              return String(request); // may throw TypeError
            }
          });

          return Promise.all(
            requests.map(function(request) {
              if (typeof request === 'string') {
                request = new Request(request);
              }

              var scheme = new URL(request.url).protocol;

              if (scheme !== 'http:' && scheme !== 'https:') {
                throw new NetworkError("Invalid scheme");
              }

              return fetch(request.clone());
            })
          );
        }).then(function(responses) {
          // TODO: check that requests don't overwrite one another
          // (don't think this is possible to polyfill due to opaque responses)
          return Promise.all(
            responses.map(function(response, i) {
              return cache.put(requests[i], response);
            })
          );
        }).then(function() {
          return undefined;
        });
      };
    }

    if (!CacheStorage.prototype.match) {
      // This is probably vulnerable to race conditions (removing caches etc)
      CacheStorage.prototype.match = function match(request, opts) {
        var caches = this;

        return this.keys().then(function(cacheNames) {
          var match;

          return cacheNames.reduce(function(chain, cacheName) {
            return chain.then(function() {
              return match || caches.open(cacheName).then(function(cache) {
                  return cache.match(request, opts);
                }).then(function(response) {
                  match = response;
                  return match;
                });
            });
          }, Promise.resolve());
        });
      };
    }

    if ('serviceWorker' in navigator) {
      // Delay registration until after the page has loaded, to ensure that our
      // precaching requests don't degrade the first visit experience.
      // See https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/registration
      window.addEventListener('load', function() {
        // Your service-worker.js *must* be located at the top-level directory relative to your site.
        // It won't be able to control pages unless it's located at the same level or higher than them.
        // *Don't* register service worker file in, e.g., a scripts/ sub-directory!
        // See https://github.com/slightlyoff/ServiceWorker/issues/468
        navigator.serviceWorker.register('sw.js').then(function(reg) {
          // updatefound is fired if service-worker.js changes.
          reg.onupdatefound = function() {
            // The updatefound event implies that reg.installing is set; see
            // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event
            var installingWorker = reg.installing;

            installingWorker.onstatechange = function() {
              switch (installingWorker.state) {
                case 'installed':
                  if (navigator.serviceWorker.controller) {
                    // At this point, the old content will have been purged and the fresh content will
                    // have been added to the cache.
                    // It's the perfect time to display a "New content is available; please refresh."
                    // message in the page's interface.
                    console.log('New or updated content is available.');
                  } else {
                    // At this point, everything has been precached.
                    // It's the perfect time to display a "Content is cached for offline use." message.
                    console.log('Content is now available offline!');
                  }
                  break;

                case 'redundant':
                  console.error('The installing service worker became redundant.');
                  break;
              }
            };
          };
        }).catch(function(e) {
          console.error('Error during service worker registration:', e);
        });
      });
    }
  }
</script>
